package database;

public class DatabaseNumber extends DatabaseObject {
    public static final String LOWER_BOUND = "lower-bound";
    public static final String UPPER_BOUND = "upper-bound";
    private int lowerBound = Integer.MIN_VALUE;
    private int upperBound = Integer.MAX_VALUE;

    public DatabaseNumber() {
        value = 0;
    }

    @Override
    public void setParam(String param, Object value) {
        if (param.equals(LOWER_BOUND)) {
            lowerBound = Integer.parseInt((String) value);
        }
        if (param.equals(UPPER_BOUND)) {
            upperBound = Integer.parseInt((String) value);
        }
    }

    @Override
    public Object getParam(String param) {
        if (param.equals(LOWER_BOUND)) {
            return (Object) lowerBound;
        }
        if (param.equals(UPPER_BOUND)) {
            return (Object) upperBound;
        }
        return null;
    }

    @Override
    public void setValue(Object value) {
        try {
            this.value = (Object) Integer.parseInt((String) value);
        } catch (NumberFormatException nfe) {
            this.value = null;
        }
    }

    @Override
    public String toString() {
        return value.toString();
    }

    @Override
    protected void acceptValidator(Validator v) {
        v.validateNumber(this);
    }

    @Override
    public int compareTo(DatabaseObject o) {
        Integer i1 = (Integer) value;
        Integer i2 = (Integer) o.getValue();
        return i1.compareTo(i2);
    }

}
